﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class PersonCenter : System.Web.UI.Page
{
    protected BLL.LostAndFound blllaf = new BLL.LostAndFound();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //登录了
            if (common.isLoginOrNot())
            {
                Bind(1);
            }
            else
            {
                MessageBox.Show(this, "请先登录");
                MessageBox.script(this,"window.location='index.aspx';");
            }
        }
    }

    protected void Bind(int pagenum)
    {
        string lostproperty = "总信息";//默认为总信息
        if (ViewState["info"] != null)
        {
            lostproperty = ViewState["info"].ToString();
        }
        int uid=((MODEL.Users)Session["currentUser"]).UId;
        //得到总页数
        int count = blllaf.getPCByUserId(uid, lostproperty);
        if (pagenum < 1)
        {
            pagenum = 1;
        }
        if (pagenum > count)
        {
            pagenum = count;
        }
        //别要忘了这个
        btnpre.Enabled = true;
        btnnext.Enabled = true;
        if (pagenum == 1)
        {
            btnpre.Enabled = false;
        }
        if (pagenum == count)
        {
            btnnext.Enabled = false;
        }
        
        string ordercondition = " [Time] desc";
        if (ViewState["order"] != null)
        {
            ordercondition = ViewState["order"].ToString();
        }
        List<MODEL.LostAndFound> models = blllaf.getModelsByUserId(uid, pagenum, lostproperty,ordercondition);
        foreach (MODEL.LostAndFound laf in models)
                {
                    if(laf.Feature.Length>12)
                    laf.Feature = laf.Feature.Substring(0, 12)+"......";
                }
        rpPersonCenter.DataSource = models;
        rpPersonCenter.DataBind();
        pageNum.Text = pagenum.ToString();
        pageCount.Text = count.ToString();
    }
    //下一页
    protected void btnnext_Click(object sender, EventArgs e)
    {
        int currentpage = Convert.ToInt32(pageNum.Text);
        currentpage++;
        Bind(currentpage);
    }
    //上一页
    protected void btnpre_Click(object sender, EventArgs e)
    {
        int currentpage = Convert.ToInt32(pageNum.Text);
        currentpage--;
        Bind(currentpage);
    }
    //跳转
    protected void btnjump_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int pagenum = Convert.ToInt32(txtjump.Text);
            Bind(pagenum);
        }
    }
    //按时间
    protected void orderByTime_Click(object sender, EventArgs e)
    {
        if (orderByTime.Text == "时间↑")
        {
            orderByTime.Text = "时间↓";
            ViewState["order"] = " [Time]";
            Bind(1);
        }
        else
        {
            orderByTime.Text = "时间↑";
            ViewState["order"] = " [Time] desc";
            Bind(1);
        }
    }
    //dropdownlist发生改变
    protected void ddlLostProperty_SelectedIndexChanged(object sender, EventArgs e)
    {
        ViewState["info"] = ddlLostProperty.SelectedItem.Text;
        Bind(1);
    }
}